package com.sbhotel.chotellauncher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.entity.AppBean;
import com.sbhotel.chotellauncher.util.ScreenUtils;

import java.util.List;

/**
 * @author leihuiming
 * @Description
 */
public class AllAppAdapter extends RecyclerView.Adapter<AllAppAdapter.ViewHolder> {

    private Context context;
    List<AppBean> list;

    public AllAppAdapter(Context context, List<AppBean> data) {
        this.context = context;
        list = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app, parent, false);

        ViewHolder holder = new ViewHolder(v);
        ViewGroup.LayoutParams layoutParams = holder.container.getLayoutParams();
        layoutParams.width = (ScreenUtils.getScreenWidth(context)/* - 256*/) / 5;//
        layoutParams.height = layoutParams.width;
        Log.d("showboom", "layoutParams.width1 =" + layoutParams.width);
        holder.container.setLayoutParams(layoutParams);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        ViewGroup.LayoutParams layoutParams = holder.container.getLayoutParams();
        layoutParams.width = (ScreenUtils.getScreenWidth(context) /*- 256*/) / 5;//
        holder.container.setLayoutParams(layoutParams);
        holder.itemTitle.setText(list.get(position).getName());
        holder.itemImg.setImageDrawable(list.get(position).getIcon());
        holder.itemRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickLitener != null) {
                    mOnItemClickLitener.onItemClick(holder.itemRL, list.get(position), position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView itemTitle;
        public ImageView itemImg;
        public RelativeLayout container;

        public RelativeLayout getItemRL() {
            return itemRL;
        }

        public RelativeLayout itemRL;

        public ViewHolder(View itemView) {
            super(itemView);
            itemTitle = (TextView) itemView.findViewById(R.id.item_app_name);
            itemImg = itemView.findViewById(R.id.item_app_img);
            itemRL = (RelativeLayout) itemView.findViewById(R.id.itemRL);
            container = itemView.findViewById(R.id.container_layout);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, Object data, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
